package com.lebsh.floor.show.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MainView extends Composite {
	private FlexTable layout = new FlexTable();
	private SimplePanel header = new SimplePanel();
	private SimplePanel center = new SimplePanel();
	private Labels labelResource = GWT.create(Labels.class);
	private AppImageResource imageResource = GWT.create(AppImageResource.class);
	private AppHTMLResource htmlResource = GWT.create(AppHTMLResource.class);
	private EnvLinkLabel     howItWorks;
	private ImageLabelButton pageChooser;
	private DialogBox aboutDialog;
	private DecoratedPopupPanel contuctUsPopup;
	private EnvLinkLabel contactUs;

	

	public MainView() {
		SimplePanel panel = new SimplePanel();
		panel.setSize("100%", "100%");
		panel.setWidget(layout);
		layout.setStyleName("direction");
		initWidget(layout);
		iniAboutDialog();
		initContuctUsDialog();
		initHeader();
		layout.setWidget(0, 0, header);
		layout.setWidget(1, 0, new HTML(htmlResource.getInstractionsHtml().getText()));
		layout.getFlexCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		layout.setWidget(2, 0, center);
		layout.getFlexCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_TOP);
		layout.getFlexCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_TOP);
		layout.getFlexCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_TOP);
//		center.setSize("100%", "100%");
		center.setWidget(new ImageWithLoadIndicator(imageResource.getMainPage()));
//		header.setWidth("100%");
		layout.setSize("100%", "100%");
		initHandlers();
	}
	

	private void iniAboutDialog() {
		final Button closeButton = new Button(labelResource.close());
		closeButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				aboutDialog.hide();
				
			}
		});
		aboutDialog = new DialogBox();
		aboutDialog.setAnimationEnabled(true);
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(new HTML(htmlResource.getIntroHtml().getText()));
		dialogVPanel.add(closeButton);
		aboutDialog.setWidget(dialogVPanel);
		
	}
	private void initContuctUsDialog() {
		contuctUsPopup = new DecoratedPopupPanel(true);
		contuctUsPopup.setAnimationEnabled(true);
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(new HTML(htmlResource.getContuctUsHtml().getText()));
		contuctUsPopup.setWidget(dialogVPanel);
		
	}


	private void initHeader() {
		FlexTable headerLayout = new FlexTable();
		howItWorks = new EnvLinkLabel(labelResource.howItWorks());
		contactUs = new EnvLinkLabel(labelResource.contactUs());
//		contactUs = new HTML("<a href=mailto:"+labelResource.customerContactMail()+ "?subject="+labelResource.customerContactSubject()+">"+labelResource.contactUs()+"</a>");
		header.setWidget(headerLayout);

		pageChooser = new ImageLabelButton(imageResource.getLogo());
		headerLayout.setWidget(0, 0, pageChooser);
		headerLayout.setWidget(0, 1, createHeaderSeperator());
		headerLayout.setWidget(0, 2, howItWorks);
		headerLayout.setWidget(0, 3, createHeaderSeperator());
		headerLayout.setWidget(0, 4, contactUs);
	
		
	}

	private void initHandlers() {
		howItWorks.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				aboutDialog.center();
			}
		});
		contactUs.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				contuctUsPopup.setPopupPosition(event.getClientX()+10, event.getClientY()+10);
				contuctUsPopup.show();
			}
		});

	}
	
	private TitledLabel createHeaderSeperator(){
		return new TitledLabel("  |  ");
	}


}
